# -*- coding: utf-8 -*-
"""
	sjtumail Tests
	~~~~~~~~~~~~

	Tests the sjtumail application.

	:copyright: (c) 2010 by Armin Ronacher.
	:license: BSD, see LICENSE for more details.
"""
import os
import sjtumail
import unittest
import tempfile
from flask import jsonify
import simplejson



class sjtumailTestCase(unittest.TestCase):

	def setUp(self):
		"""Before each test, set up a blank database"""
		#self.db_fd, sjtumail.app.config['DATABASE'] = tempfile.mkstemp()
		self.app = sjtumail.app.test_client()
		sjtumail.connect_db()
		#sjtumail.init_db()

	def tearDown(self):
		"""Get rid of the database again after each test."""
		#os.close(self.db_fd)
		#os.unlink(sjtumail.app.config['DATABASE'])

	def login(self):
		return self.app.post('/login?username=pcj&password=111111'
			, follow_redirects=True)

	def logout(self):
		return self.app.get('/logout', follow_redirects=True)
		
	def test_group(self):
		"""test group"""
		#self.app.post('/group/getInfo?groupid=1')
		#self.app.post('/group/getPublicList')
		#self.login()
		#self.app.post('/group/member/getList?groupid=1')
		#self.logout()
		#self.login()
		#self.app.post('/group/member/add?address=zzq@gmail.com&groupid=1')
		#self.logout()
		#self.login()
		#self.app.post('/group/member/remove?address=zzq@gmail.com&groupid=1')
		#self.app.post('/group/getListn?ownername=pcj')
		#self.app.post('/group/add?name=pcjgroup&topic=pcjtopic&policy=0')
		#self.app.post('/group/remove?groupid=7')
		#self.app.post('/group/modify?groupid=1&topic=pcjtopic')
		#self.logout()
		
	def test_user(self):
		"""test user"""
		#self.app.post('/usr/getInfo?address=pcj@gmail.com')
		#self.login()
		#self.app.post('/user/modify?address=panchenji@gmail.com')
		#self.logout()
	
	def test_address(self):
		"""test mail"""
		#self.login()
		#self.app.post('/address/listmailaddressn?groupname=group1')
		#self.logout()
		
	def test_mail(self):
		"""test mail"""
		#self.login()
		#self.app.post('/mail/getList?groupid=1')
		#self.app.post('/mail/getListn?groupname=group1')
		#self.app.post('/mail/remove?mailid=1')
		#self.logout()
		
		#print data
		#self.app.post('/mail/sendmail',data=dict(
		#				sender='panchenji@sjtu.com',
		#				content='hello',
		#				subject='hello',
		#				receiver= 'panchenji@hotmail.com'))
		self.login()
		self.app.post('/mail/sendtogroup?sender=panchenji@sjtu.com&content=test&subject=test&groupid=1')
		self.logout()
	'''
	def test_register(self):
		#return self.app.post('/user/register?address',data=dict(
		#	address='dsdsd@gmail.com', 
		#	passwd='1a2a3a', 
		#	name='abxcdf',
		#	full_name='fdsfdf'
		#	))
		return self.app.post('/user/register?address=pcj@gmail.com&passwd=111111&name=pcj&fullname=panchenji')
		# testing functions
	'''
	'''
	def test_empty_db(self):
		"""Start with a blank database."""
		rv = self.app.get('/')
		assert 'No entries here so far' in rv.data
	'''

	
	def test_login_logout(self):
		"""Make sure login and logout works"""
		#self.test_register()
		#rv = self.login()
		#assert 'You were logged in' in rv.data
		#rv = self.logout()
		
		#assert 'You were logged out' in rv.data
		#rv = self.login(sjtumail.app.config['USERNAME'] + 'x',
		#	sjtumail.app.config['PASSWORD'])
		#assert 'Invalid username' in rv.data
		#rv = self.login(sjtumail.app.config['USERNAME'],
		#	sjtumail.app.config['PASSWORD'] + 'x')
		#assert 'Invalid password' in rv.data
	'''
	def test_messages(self):
		"""Test that messages work"""
		self.login(sjtumail.app.config['USERNAME'],
			sjtumail.app.config['PASSWORD'])
		rv = self.app.post('/add', data=dict(
			title='<Hello>',
			text='<strong>HTML</strong> allowed here'
			), follow_redirects=True)
		assert 'No entries here so far' not in rv.data
		assert '&lt;Hello&gt;' in rv.data
		assert '<strong>HTML</strong> allowed here' in rv.data
		'''

if __name__ == '__main__':
	unittest.main()
